package com.auto.admin.util;

import java.util.Arrays;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @author liangshf
 * @Description: http请求工具类
 * @date 2019-06-20
 */
public class RestTemplateUtil {

    /**
     * 实际执行请求的template
     */
    private static RestTemplate restTemplate = new RestTemplate();

    /**
     * json请求
     */
    public static <T> T request(String url, HttpMethod httpMethod, MultiValueMap<String, String> headers, Object params,
        Class<T> cls) {
        HttpHeaders httpHeaders = new HttpHeaders(headers);
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        if (httpMethod.name().equals(HttpMethod.POST.name())) {
            HttpEntity<Object> request = new HttpEntity<>(params, httpHeaders);
            return executePost(url, request, cls);
        } else {
            HttpEntity<Object> request = new HttpEntity<>(httpHeaders);
            return executeGet(url + params, request, cls);
        }
    }

    /**
     * json请求
     */
    public static <T> T post(String url, Object entity, Class<T> cls) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Object> request = new HttpEntity<>(entity, httpHeaders);
        return executePost(url, request, cls);
    }

    /**
     * 表单请求
     */
    public static <T> T formPost(String url, MultiValueMap<String, Object> params, Class<T> cls) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<Object> request = new HttpEntity<>(params, headers);
        return executePost(url, request, cls);
    }

    /**
     * post 请求
     */
    private static <T> T executePost(String url, HttpEntity<Object> request, Class<T> cls) {
        ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.POST, request, cls);
        return exchange.getBody();
    }

    /**
     * get 请求
     */
    private static <T> T executeGet(String url, HttpEntity<Object> request, Class<T> cls) {
        ResponseEntity<T> exchange = restTemplate.exchange(url, HttpMethod.GET, request, cls);
        return exchange.getBody();
    }

    /**
     * get 请求
     */
    public static <T> T get(String url, Map<String, Object> params, Class<T> cls) {
        StringBuilder urlBuilder = new StringBuilder(url);
        urlBuilder.append("?");
        if (!CollectionUtils.isEmpty(params)) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        return restTemplate.getForObject(urlBuilder.toString(), cls);
    }

}